/**
 * 工程名：COMMON
 * 包名：com.mt.common.safe
 * 文件名：User.java
 */
package com.mt.system.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.mt.component.domain.Common;
import com.mt.utils.StringUtils;



/**
 * 类名称：User
 * 类描述：系统用户
 * 创建人：马腾
 * 创建时间：2012-10-10
 */
@Entity
@Table(name="CFG_SYS_USER")
public class User extends Common implements Serializable {
	
	private static final long serialVersionUID = 7968337645707320696L;
	
	private String name;//用户名称
	private String password;//密码
	private String sex;
	private String phone;//用户的电话
	private String email;//用户的邮箱
	private String address;//用户的地址
	private Date operateTime;
	private Collection<Role> roles;//该用户拥有的所有角色
	private int status = 1;//状态等于1:正常,0:已经删除
	
	@Column(name="USER_NAME" , length=32 , nullable=false , unique=true)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="USER_PASSWORD" , length=32 , nullable=false)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name="USER_SEX" , length=8)
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Column(name="USER_PHONE" , length=16)
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(name="USER_MAIL" , length=64)
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Column(name="USER_ADDRESS" , length=128)
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="OPERATE_TIME")
	public Date getOperateTime() {
		return operateTime;
	}
	public void setOperateTime(Date operateTime) {
		this.operateTime = operateTime;
	}
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name = "CFG_SYS_USER_ROLE",
		joinColumns = {@JoinColumn(name = "USER_ID", nullable = false, updatable = false)}, 
		inverseJoinColumns = {@JoinColumn(name = "ROLE_ID", nullable = false, updatable = false)}
	)
	public Collection<Role> getRoles() {
		return roles;
	}
	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}
	
	@Column(name="USER_STATUS" , length=4)
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof User){
			User user = (User)obj;
			if(StringUtils.isEmpty(user.getId())) return false;
			return user.getId().equals(getId());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getId()==null ? 0 : getId().hashCode();
	}
}
